package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.ParkComponentDao;
import cn.topevery.garden.entity.fetch.ParkComponentFetch;
import cn.topevery.garden.entity.po.ParkComponent;
import cn.topevery.garden.entity.query.ParkComponentQuery;
import cn.topevery.garden.entity.vo.ParkComponentVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.DbScriptHelper;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * 
 *
 * @author code-generate
 * @date 2022-01-05
 */
@Repository
public class ParkComponentDaoImpl extends BaseDaoImpl<ParkComponent> implements ParkComponentDao {
    @Override
    public <P extends DbCsp> List<ParkComponent> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<ParkComponentVO> listPage(ParkComponentQuery query) {
        /*String table = "t_park_component tpc";
        String columns = "tpc.*";
        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;
        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tpc.c_db_create_date desc");
        sqlQueryMate
                .where(true, "tpc.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotBlank(query.getComponentType()), "tpc.c_component_type", ScriptConditionEnum.Eq, query.getComponentType())
        ;
        return getDataPager(sqlQueryMate, pageSize, pageIndex, ParkComponentVO.class);*/
        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;
        StringBuilder sqlBuilder = new StringBuilder(" tpc.c_db_status = 0 ");
        if (StringUtils.isNotBlank(query.getComponentType())) {
            sqlBuilder.append(" and tpc.c_component_type = '" + query.getComponentType()+"'");
        }
        if (StringUtils.isNotBlank(query.getKeywords())) {
            sqlBuilder.append(" and ( tpc.c_name like concat ('%','" + query.getKeywords() + "','%')" +
                    " or tpc.c_street_name like concat ('%','" + query.getKeywords() + "','%')" +
                    " or tpc.c_community_name like concat ('%','" + query.getKeywords() + "','%') )");
        }
        String sqlPage = DbScriptHelper.getSelectSqlPage("t_park_component tpc", "tpc.*",
                sqlBuilder.toString(), null, pageSize, pageIndex);
        DataPager<ParkComponentVO> dataPagerByCustomize = getDataPagerByCustomize(sqlPage, CommandType.Text, null, ParkComponentVO.class);
        return  dataPagerByCustomize;
    }

    @Override
    public ParkComponent getEntityByColumn(String key,String value){
        KeyValue<String> map=new KeyValue<>();
        map.key=key;
        map.value=value;
        return getEntityByColumnEqValue(map);
    }

    @Override
    public List<Map<String, Object>> getComponentTypeList() {
        String sql= "SELECT DISTINCT c_component_type componentType,c_obj_name objName FROM t_park_component ORDER BY c_obj_name\n";

        return getMap(sql);
    }

    @Override
    public DataPager<ParkComponent> queryParkComponentPage(ParkComponentFetch parkComponentFetch) {
        SqlQueryMate sqlQueryMate = new SqlQueryMate("t_park_component").setColumns("*");
        sqlQueryMate.where(true, "c_db_status", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotBlank(parkComponentFetch.getComponentType()), "c_component_type", ScriptConditionEnum.Eq, parkComponentFetch.getComponentType());
        int pageIndex = parkComponentFetch.getPageIndex() - 1;
        int pageSize = parkComponentFetch.getPageSize();
        return getDataPager(sqlQueryMate, pageSize, pageIndex, ParkComponent.class);
    }

}
